package com.helios.news.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.helios.core.bean.Result;
import com.helios.news.bean.Notice;
import com.helios.news.service.NoticeService;

/**
 * 信息通告控制器.
 * 
 * @author liyu
 */
@RestController
@RequestMapping("notice")
public class NoticeController {
	
	@Autowired
	private NoticeService noticeService;

	/**
	 * 信息通告列表页.
	 */
	@RequestMapping(value = "list.html")
	public ModelAndView toInfoList() {
		ModelAndView modelAndView = new ModelAndView("news/notice_list");
		return modelAndView;
	}
	
	/**
	 * 信息通告列表接口.
	 */
	@RequestMapping(value = "list.json")
	public Result getInfoList(HttpServletRequest request) {
		Result result = new Result();
		
		int page = NumberUtils.toInt(request.getParameter("page"), 1);
		int rows = NumberUtils.toInt(request.getParameter("rows"), 10);
		
		List<Notice> beans = noticeService.gets(page, rows);
		int count          = noticeService.getCount();
		
		result.putMessage("beans", beans);
		result.putMessage("page", page);
		result.putMessage("count", count);
		return result;
	}
	
	@RequestMapping(value = "add.html")
	public ModelAndView toAdd(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("news/notice_add");
		int id = NumberUtils.toInt(request.getParameter("id"), -1);
		if(id != -1){
			modelAndView.addObject("bean", noticeService.get(id));			
		}
		modelAndView.addObject("index", NumberUtils.toInt(request.getParameter("index"), -1));
		return modelAndView;
	}
	
	@RequestMapping(value = "delete.json")
	public Result delete(HttpServletRequest request){
		Result result = new Result();
		int id = NumberUtils.toInt(request.getParameter("id"), -1);
		if(id == -1){
			result.setCode(-1);
			result.putInfo("参数不合法");
		}else{
			boolean bol = noticeService.delete(id);
			if(bol){
				result.setCode(0);
				result.putInfo("");
			}else{
				result.setCode(-1);
				result.putInfo("删除失败");
			}
		}
		return result;
	}
	
	@RequestMapping(value = "update.json")
	public Result update(HttpServletRequest request){
		Result result = new Result();
		
		Notice notice;
		int id = NumberUtils.toInt(request.getParameter("id"), -1);
		if(id == -1){
			notice = new Notice();
		}else{
			notice = noticeService.get(id);
			if(notice == null) notice = new Notice();
		}
		
		String title = request.getParameter("title");
		String context = request.getParameter("context");
		
		if(StringUtils.trimToNull(title) == null){
			result.setCode(-1);
			result.putInfo("标题不能为空!");
			return result;
		}
		
		if(StringUtils.trimToNull(context) == null){
			result.setCode(-1);
			result.putInfo("内容不能为空!");
			return result;
		}
		
		notice.setTitle(title);
		notice.setContext(context);
		notice.setTime(new Date());
		
		boolean bol = noticeService.save(notice);
		if(bol){
			result.setCode(0);
			result.putInfo("");
		}else{
			result.setCode(-1);
			result.putInfo("提交失败");
		}
		return result;
	}
}
